import 'package:flutter/material.dart'
    hide RefreshIndicator, RefreshIndicatorState;
import 'package:lottie/lottie.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

/// lottie动画下拉刷新头部
class GTRefreshHeader extends StatefulWidget {
  final String animationPath;

  const GTRefreshHeader({
    super.key,
    this.animationPath = 'packages/lib_uikit/assets/json/gt_refresh_header.json',
  });

  @override
  State<StatefulWidget> createState() {
    return _GTRefreshHeaderState();
  }
}

class _GTRefreshHeaderState extends State<GTRefreshHeader>
    with SingleTickerProviderStateMixin {
  late Widget _icon;
  AnimationController? _controller;

  @override
  void initState() {
    super.initState();
    _controller = AnimationController(
        vsync: this,
        duration: const Duration(
          seconds: 1,
        ));
    if (mounted) {
      _icon = SizedBox(
        width: 20,
        height: 20,
        child: Lottie.asset(
          widget.animationPath,
          controller: _controller,
        ),
      );
    }
  }

  @override
  void dispose() {
    _controller?.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return CustomHeader(
      onOffsetChange: (offset) {
        if (_controller?.isAnimating != true) {
          //modeChange触发的 高度是80
          _controller?.value = offset / 80.0;
        }
      },
      onModeChange: (mode) {
        if (mode == RefreshStatus.idle) {
          _controller?.reset();
        } else {
          if (_controller?.isAnimating != true) {
            _controller?.repeat();
          }
        }
      },
      builder: (BuildContext context, RefreshStatus? mode) {
        return SizedBox(
          height: 60,
          child: Center(
            child: _icon,
          ),
        );
      },
    );
    // 待删除
    // return ClassicHeader(
    //   releaseText: '',
    //   refreshingText: '',
    //   completeText: '',
    //   idleText: '',
    //   failedText: '',
    //   failedIcon: _icon,
    //   completeIcon: _icon,
    //   idleIcon: _icon,
    //   refreshingIcon: _icon,
    //   releaseIcon: _icon,
    // );
  }
}
